/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package testapplication.sprite;

import java.lang.reflect.Constructor;

import rawengine.gamecore.GameCore;
import rawengine.graphics.Texture;
import rawengine.graphics.animation.Animation;
import rawengine.graphics.animation.AnimationState;
import rawengine.math.Vector2f;
import rawengine.tilegame.sprite.Sprite;

/**
 *
 * @author Fabio
 */
public abstract class Creature extends Sprite {

    /**
        Amount of time to go from STATE_DYING to STATE_DEAD.
    */
    protected static final int DIE_TIME = 1000;

    public static final int STATE_NORMAL = 0;
    public static final int STATE_DYING = 1;
    public static final int STATE_DEAD = 2;

    protected Animation walk;
    protected Animation dead;
    private int state;
    protected long stateTime;
    protected Animation newAnim;

    /**
        Creates a new Creature with the specified Animations.
    */
  
    public Creature(String name, Texture image, Vector2f boundsCollision)
           
        {
            super(name, image, boundsCollision);
            state = STATE_NORMAL;
            
        }
    


    @SuppressWarnings("rawtypes")
	@Override
    public Object clone() {
        // use reflection to create the correct subclass
        Constructor constructor = getClass().getConstructors()[0];
        try {
            return constructor.newInstance(new Object[] { 
                 getName(),
                 getImage(),
                 boundsCollision});
        }
        catch (Exception ex) {
            // should never happen
            ex.printStackTrace();
            return null;
        }
    }


    /**
        Gets the maximum speed of this Creature.
    */
    public float getMaxSpeed() {
        return 0;
    }


    /**
        Wakes up the creature when the Creature first appears
        on screen. Normally, the creature starts moving left.
    */
    public void wakeUp() {
        if (getState() == STATE_NORMAL && getVelocityX() == 0) {
            setVelocityX(-getMaxSpeed());
        }
    }


    /**
        Gets the state of this Creature. The state is either
        STATE_NORMAL, STATE_DYING, or STATE_DEAD.
    */
    public int getState() {
        return state;
    }


    /**
        Sets the state of this Creature to STATE_NORMAL,
        STATE_DYING, or STATE_DEAD.
    */
    public void setState(int state) 
    {
        if (this.state != state) 
        {
            this.state = state;
            stateTime = 0;
            if (state == STATE_DYING) 
            {
                setVelocityX(0);
                setVelocityY(0);
            }
            
        }
    }


    /**
        Checks if this creature is alive.
    */
    public boolean isAlive() {
        return (state == STATE_NORMAL);
    }


    /**
        Checks if this creature is flying.
    */
    public boolean isFlying() {
        return false;
    }


    /**
        Called before update() if the creature collided with a
        tile horizontally.
    */
    public void collideHorizontal() {
        setVelocityX(-getVelocityX());
    }


    /**
        Called before update() if the creature collided with a
        tile vertically.
    */
    public void collideVertical() {
        setVelocityY(0);
    }
    
    public void setAnimations()
    {
    	setWalk();
    	setDead();
    	newAnim = getWalk();
    	
    }


    /**
        Updates the animaton for this creature.
    */
    public void update() {
        // select the correct Animation
    	updateLifeTime();
             
        if (getVelocityX() < 0) {
        	walk.setState(AnimationState.NORMAL);
        	newAnim = walk;
        }
        else if (getVelocityX() > 0) {
        	walk.setState(AnimationState.MIRROR);
            newAnim = walk;
        }
        
        if (state == STATE_DYING && walk.getState() == AnimationState.NORMAL) {
        	dead.setState(AnimationState.FLIPPED);
            newAnim = dead;
        }
        else if (state == STATE_DYING && walk.getState() == AnimationState.MIRROR) {
        	dead.setState(AnimationState.MIRROR_FLIPPED);
            newAnim = dead;
        }
       

        // update the Animation
        if (getCurrentAnimation() != newAnim) {
            setCurrentAnimation(newAnim);
            getCurrentAnimation().start();
        }
        else {
            getCurrentAnimation().update();
        }

        // update to "dead" state
        stateTime += GameCore.timeHandler.getFrameTime();
        if (state == STATE_DYING && stateTime >= DIE_TIME) {
            setState(STATE_DEAD);
        }
    }



	public Animation getWalk() 
	{
		return walk;
	}



	public void setWalk() 
	{
		walk = getAnimation("walk");
	}



	public Animation getDead() 
	{
		return dead;
	}



	public void setDead() {
		this.dead = getAnimation("dead");
	}

}
